package com.spzx.payment.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.order.api.RemoteGetByOrderNo;
import com.spzx.order.api.domain.OrderInfo;
import com.spzx.payment.config.AlipayConfig;
import com.spzx.payment.domain.PaymentInfo;
import com.spzx.payment.mapper.PaymentInfoMapper;
import com.spzx.payment.service.AlipayService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ClassName: AlipayServiceImpl
 * Description:
 *
 * @Author linz
 * @Creat 2025/3/25 18:35
 * @Version 1.00
 */

@Service
public class AlipayServiceImpl implements AlipayService {
    @Autowired
    private RemoteGetByOrderNo remoteGetByOrderNo;
    @Autowired
    private AlipayClient alipayClient;
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Override
    @SneakyThrows
    public String submitAlipay(String orderNo) {

        //判断支付日志是否已经记录
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, orderNo));
        if(paymentInfo != null){
            return null;
        }

        //获取订单信息
        R<OrderInfo> OrderInfoResult = remoteGetByOrderNo.getByOrderNo(orderNo, SecurityConstants.INNER);
        if(R.FAIL == OrderInfoResult.getCode()){
            throw new ServiceException("远程调用失败：" + OrderInfoResult.getMsg());
        }
        OrderInfo orderInfo = OrderInfoResult.getData();
        String content = orderInfo.getOrderItemList().get(0).getSkuName() + "等" + orderInfo.getOrderItemList().size() + "件商品";

        //记录支付日志
        paymentInfo = new PaymentInfo();
        paymentInfo.setUserId(SecurityContextHolder.getUserId());
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPayType(2); //支付宝
        paymentInfo.setPaymentStatus(0); //未支付
        paymentInfo.setAmount(orderInfo.getTotalAmount()); //总金额
        paymentInfo.setCreateBy(SecurityContextHolder.getUserName());
        paymentInfo.setUpdateBy(SecurityContextHolder.getUserName());
        paymentInfo.setDelFlag("0");
        paymentInfo.setRemark(orderInfo.getRemark());
        paymentInfo.setContent(content);

        paymentInfoMapper.insert(paymentInfo);


        //组装支付订单的参数
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        //组装公共参数的可选参数
        request.setReturnUrl(AlipayConfig.return_payment_url);
        request.setNotifyUrl(AlipayConfig.notify_payment_url);

        //组装业务参数
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        // 设置商户订单号
        model.setOutTradeNo(orderNo);

        // 设置订单总金额
        model.setTotalAmount(orderInfo.getTotalAmount().toString());

        // 设置订单标题
        model.setSubject(content);

        // 设置产品码
        model.setProductCode("QUICK_WAP_WAY");

        //将业务参数设置在请求参数中
        request.setBizModel(model);

        //调用pageExecute生成表单字符串
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
        String form = response.getBody();
        return form;
    }
}
